package com.haina.shop.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.haina.shop.constant.MessageConstant;
import com.haina.shop.domain.IndexImg;
import com.haina.shop.domain.Prod;
import com.haina.shop.dto.ProdDTO;
import com.haina.shop.domain.ProdTag;
import com.haina.shop.domain.ShopDetail;
import com.haina.shop.dto.ProdDTO;
import com.haina.shop.mapper.ProdTagMapper;
import com.haina.shop.mapper.ShopDetailMapper;
import com.haina.shop.result.IPageIndexImg;
import com.haina.shop.result.ServiceResult;
import com.haina.shop.service.ProdService;
import com.haina.shop.mapper.ProdMapper;
import com.haina.shop.vo.ProdTagVO;
import com.haina.shop.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author 48077
* @description 针对表【tz_prod(商品)】的数据库操作Service实现
* @createDate 2024-07-31 18:34:29
*/
@Service
public class ProdServiceImpl extends ServiceImpl<ProdMapper, Prod>
    implements ProdService{
    @Autowired
    private ProdMapper mapper;
    @Autowired
    private ProdTagMapper prodTagMapper;
    @Autowired
    private ShopDetailMapper shopDetailMapper;
    /**
     * 获取新品推荐商品列表
     * @param dto
     */
    @Override
    public ServiceResult<IPageIndexImg<ProductVO>> listData(ProdDTO dto) {
        //获取当前页码
        Long currentPage = dto.getCurrent();
        //获取每页显示的条数
        Long size= dto.getSize();
        //设置分页初始值
        PageHelper.startPage(Math.toIntExact(currentPage), Math.toIntExact(size));
        //调用mapper层进行分页查询
        Page<Prod> prodPage=mapper.pageByNew(dto);
        //创建IPageIndexImg对象
        IPageIndexImg<ProductVO> iPageIndexImg=new IPageIndexImg<>();
        iPageIndexImg.setSize(size);
        iPageIndexImg.setTotal(prodPage.getTotal());
        iPageIndexImg.setCurrent(currentPage);
        iPageIndexImg.setPages(prodPage.getTotal()/size+1);
        List<ProductVO> productVOList=new ArrayList<>();
        for (Prod prod : prodPage) {
            ProductVO productVO = getVo(prod);
            productVOList.add(productVO);
        }
        iPageIndexImg.setRecords(productVOList);
         return ServiceResult.success(iPageIndexImg);
    }
    /**
     * 获取所有的商品分组列表
     */
    @Override
    public ServiceResult<List<ProdTagVO>> getTagList() {
        List<ProdTag> prodTags = prodTagMapper.selectList(null);
        List<ProdTagVO> prodTagVOList=new ArrayList<>();
        for (ProdTag tag : prodTags) {
            ProdTagVO vo = getVO(tag);
            prodTagVOList.add(vo);
        }
        return ServiceResult.success(prodTagVOList);
    }
    /**
     * 通过分组标签id（tagId）获取商品列表
     * @param tagId
     * @param size
     */
    @Override
    public ServiceResult<IPageIndexImg<ProductVO>> getTagListById(Long tagId, Long size) {
        //设置分页初始值
        PageHelper.startPage(Math.toIntExact(MessageConstant.PAGE_CURRENT), Math.toIntExact(size));
        //调用mapper层进行分页查询
        Page<Prod> prodPage=mapper.page(tagId);
        //创建IPageIndexImg对象
        IPageIndexImg<ProductVO> iPageIndexImg=new IPageIndexImg<>();
        iPageIndexImg.setSize(size);
        iPageIndexImg.setTotal(prodPage.getTotal());
        iPageIndexImg.setCurrent(MessageConstant.PAGE_CURRENT);
        iPageIndexImg.setPages( prodPage.getTotal()/size+1);
        //获取所有记录
        List<Prod> result = prodPage.getResult();
        List<ProductVO> productVOList=new ArrayList<>();
        for (Prod prod : result) {
            ProductVO vo = getVo(prod);
            productVOList.add(vo);
        }
       iPageIndexImg.setRecords(productVOList);
        return ServiceResult.success(iPageIndexImg);
    }

    //根据Tag entity->VO
    public ProdTagVO getVO(ProdTag tag){
        return new ProdTagVO(
           tag.getId(),tag.getTitle(),tag.getSeq(),tag.getStyle()
        );
    }

    //根据Product entity->VO
    public ProductVO getVo(Prod prod){
        //根据shopId查询shop信息
        Long shopId = prod.getShopId();
        LambdaQueryWrapper<ShopDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopDetail::getShopId,shopId);
        ShopDetail shopDetail = shopDetailMapper.selectOne(queryWrapper);
        String shopName=shopDetail.getShopName();
        return new ProductVO(
                shopId,shopName,prod.getProdId(), prod.getProdName(),
                prod.getPrice(),prod.getContent(),
                prod.getOriPrice(),prod.getTotalStocks(),
                prod.getBrief(),prod.getPic(),prod.getImgs(),
                prod.getCategoryId(),null,null
        );

    }
}




